package com.yunfei.schedule.crmToApp;
import com.yunfei.bo.CrmAcceDtlBo;
import com.yunfei.bo.TblobstorageBo;
import com.yunfei.bo.TblobstorageSimpleBo;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.mapper.aiiidb.CrmAcceDtlMapper;
import com.yunfei.mapper.crmdb.TblobtSorageMapper;
import com.yunfei.utils.ComponentFactory;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.DownloadPdf;
import com.yunfei.utils.string.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class ScheduledTask {
    @Value("${file.ip.address}")
    String ipAddress;
    @Value("${file.savePath}")
    String savePath;
    @Value("${file.prexDomain}")
    String prexDomain;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTask.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

  /* @Scheduled(cron = "0 57 15 ? * *") */  //每天凌晨1点执行
   @Scheduled(cron = "0 0/3 * * * ?")   //每1分钟
  /* @Scheduled(fixedRate = 300000)*/
    public void testMyBatis() {
        System.out.println("ScheduledTask定时从crm文件服务器获取所有附件信息"+ DateUtil.getDateNow("yyyy-MM-dd HH:mm:ss"));
        int newCount=0;
        int oldCount=0;
        try {
            CrmAcceDtlMapper crmAcceDtlMapper = ComponentFactory.getBean(CrmAcceDtlMapper.class);
            TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
            TblobstorageBo queryParam=new TblobstorageBo();
            List<TblobstorageSimpleBo> list=tblobtSorageMapper.findAllCrmFiles(queryParam);//crm所有附件信息
            List<TblobstorageEntity> uppdateList=new ArrayList<>();
            //客服app所有附件信息
            int size=0;
            if(!StringUtil.isNullOrEmpty(list)){
                size=list.size();
            }
            if(size>0){
                System.out.println("总条数========================"+size);
                for(int i=0;i<size;i++){
                    TblobstorageSimpleBo tblobstorageBo=list.get(i);
                    CrmAcceDtlBo crmFileBo=new CrmAcceDtlBo();
                    String lStorageid=tblobstorageBo.getlStorageid();
                    crmFileBo.setLStorageid(lStorageid);
                    crmFileBo.setFileUrl(prexDomain);
                    String cFilePath=tblobstorageBo.getcFilepath();
                    String appPath=tblobstorageBo.getcAppPath();
                    //获取原始文件存放地址
                    String oFileUrl=ipAddress+"/"+cFilePath.replaceAll("\\\\","/")+"baccessory";
                    String fileName=tblobstorageBo.getcFilename();
                    String localFilePath=savePath+appPath;
                    localFilePath=localFilePath.replaceAll("\\\\","/")+"/";
                    boolean flag=DownloadPdf.downLoadServerFileToLocal(oFileUrl,fileName,localFilePath);
                    //文件存在，再判断是否已经添加到app服务器表里面
                    //判断文件是否已经存在
                    CrmAcceDtlBo query=new CrmAcceDtlBo();
                    query.setLStorageid(lStorageid);
                    int count=crmAcceDtlMapper.count(query);
                    if(count==0){
                        crmAcceDtlMapper.add(crmFileBo);
                        newCount++;
                       // tblobtSorageMapper.update(tblobstorageEntity);

                    }else{
                        oldCount++;
                    }
                    //更新crm附件状态为已同步c_objtype
                    TblobstorageEntity tblobstorageEntity=new TblobstorageEntity();
                    tblobstorageEntity.setlStorageid(lStorageid);
                    //tblobstorageEntity.setcObjtype(oFileUrl);
                    tblobstorageEntity.setcObjtype("Y");
                    //uppdateList.add(tblobstorageEntity);
                    tblobtSorageMapper.update(tblobstorageEntity);
                }
                //批量更新
              /*  System.out.println("批量更新条数=========================="+uppdateList.size());
                if(!StringUtil.isNullOrEmpty(uppdateList)&&uppdateList.size()>0){
                    tblobtSorageMapper.updateBatch(uppdateList);
                }*/
                try{
                    //程序暂时半秒再执行
                    Thread.sleep(500);//半秒
                    crmAcceDtlMapper.updateSubkeyId();
                }catch(Exception e){
                    //System.exit(0);//退出程序
                }

            }

            if (list != null) {
                System.out.println(list.size()+"条任务处理完成,其中新增 "+newCount+"条,已存在 "+oldCount+"条");
            } else {
                System.out.println("我什么事都不用做，等待下次再说吧");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}

